package tool;

import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import constant.ConfigConstants;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import util.ParamUtil;

/**
 * 快递
 * Created by Intellij IDEA.
 * User:  老赵
 * Date:  2023/7/5
 */
public class Express {

    public static void openExpressStage() {
        Stage expressStage = new Stage();
        expressStage.setTitle("快递查询");
        expressStage.setFullScreen(ConfigConstants.FullScreen);

        // 创建快递单号查询界面的元素
        Label trackingNumberLabel = new Label("快递单号:");
        TextField trackingNumberField = new TextField();
        Button searchButton = new Button("查询");
        // 创建一个WebView组件
        WebView webView = new WebView();
        webView.setPrefSize(500, 650);
        // 设置查询按钮的点击事件
        searchButton.setOnAction(event -> {
            String trackingNumber = trackingNumberField.getText();
            // 调用查询快递状态的方法
            String result = getExpressStatus(trackingNumber, ParamUtil.getExpressToken());
            webView.getEngine().loadContent(result);
        });
        // 创建快递单号查询界面的布局
        VBox root = new VBox(10);
        root.setPadding(new Insets(10));
        root.getChildren().addAll(trackingNumberLabel, trackingNumberField, searchButton, webView);
        // 创建快递单号查询界面的场景
        Scene scene = new Scene(root, 500, 700);
        expressStage.setScene(scene);
        expressStage.show();
    }

    public static String getExpressStatus(String trackingNumber, String token) {
        String url = "https://v2.alapi.cn/api/kd?number=" + trackingNumber + "&token=" + token;
        String html = HttpUtil.get(url);
        String result;
        JSONObject jsonObject = JSONUtil.parseObj(html);
        StringBuilder sb = new StringBuilder();
        sb.append("<div style=\"background-color: #f2f2f2; padding: 10px;\">")
                .append("<h3>快递信息：</h3>")
                .append("<div>")
                .append("<ul style=\"list-style-type: none; padding: 0;\">");
        if (HttpStatus.HTTP_OK == jsonObject.getInt("code")) {
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray array = (JSONArray) data.get("info");
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = (JSONObject) array.get(i);
                sb.append("<li style=\"margin-bottom: 10px;\">")
                        .append("<strong>时间：</strong>").append(object.getStr("time")).append("<br/>")
                        .append("<strong>物流内容：</strong>").append(object.getStr("content")).append("<br/>")
                        .append("<strong>物流状态：</strong>").append(object.getStr("status_desc")).append("<br/>")
                        .append("</li>");
            }
            result = sb.toString();
        } else {
            sb.append("<li style=\"margin-bottom: 10px;\">")
                    .append(jsonObject.getStr("msg")).append("<br/>")
                    .append("</li>");
            result = sb.toString();
        }
        return result;
    }
}
